<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" omit-xml-declaration="yes" version="5.0" indent="yes"/>
    <xsl:template match="/api-def">
        <html>
            <head>
                <title>API 文档</title>
                <link rel="stylesheet" type="text/css" href="bootstrap.css"/>
                <style type="text/css">table { table-layout: fixed; } .back-to-top { position: fixed; right: 15px; bottom: 15px; } .hovercard { position: absolute; width: 400px; /* don't ask me why */ z-index: 1; box-shadow: 0 5px 10px #ddd; } .hovercard::after { position: absolute; left: 192px; width: 0; height: 0; bottom: 100%; border-left: 8px solid transparent; border-right: 8px solid transparent; border-bottom: 8px solid #ddd; content: ''; display: block; } #alert { display: none; cursor: pointer; } samp, pre { cursor: pointer; position: relative; } .test-modal-container { position: fixed; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); left: 0; top: 0; } .test-modal { width: 50%; margin: auto; position: relative; top: 10%; max-height: 80%; overflow-y: scroll; background: #fff; }</style>
                <script type="text/javascript" language="javascript" src="hovercard.js"/>
                <script type="text/javascript" language="javascript">window.addEventListener('load', apiXmlLoaded);</script>
            </head>
            <body>
                <div class="container" id="header">
                    <xsl:apply-templates select="apis"/>
                    <xsl:apply-templates select="types"/>
                    <xsl:apply-templates select="errors"/>
                </div>
                <div class="back-to-top btn-group" role="group">
                    <a href="#errors" class="btn btn-default" role="button">错误代码</a>
                    <a href="#header" class="btn btn-primary" role="button">回到顶部</a>
                </div>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="apis">
        <div class="page-header">
            <h1>API 文档</h1>
            <p class="text-danger">1.
                <code>login</code> 为
                <code>true</code> 的所有接口需要登陆
            </p>
            <p class="text-danger">2. 接口公共路径为 https://api.pindd.365uxuan.com/wechat/</p>
            <p class="text-danger">3. 接口公共参数 openid</p>
        </div>
        <div class="alert alert-danger" role="alert" id="alert">
            <b>文档中存在错误！点此跳转至错误处。</b>
        </div>
        <div id="index" class="list-group">
            <xsl:for-each select="api">
                <a href="#" class="list-group-item">
                    <xsl:attribute name="href">#api-<xsl:value-of select="@name"/></xsl:attribute>
                    <span>
                        <xsl:if test="@status = 'unfinished'">
                            <xsl:attribute name="class">text-danger</xsl:attribute>
                        </xsl:if>
                        <xsl:if test="@status = 'lower'">
                            <xsl:attribute name="class">text-primary</xsl:attribute>
                        </xsl:if>
                        <xsl:if test="@status = 'incomplete'">
                            <xsl:attribute name="class">text-success</xsl:attribute>
                        </xsl:if>
                        <xsl:value-of select="@name"/> （
                        <xsl:value-of select="description"/> ）
                    </span>
                </a>
            </xsl:for-each>
        </div>
        <xsl:for-each select="api">
            <h2>
                <xsl:attribute name="id">api-<xsl:value-of select="@name"/></xsl:attribute>
                <xsl:value-of select="@name"/>
                <small>
                    <xsl:value-of select="description"/>
                    <xsl:if test="@exclude">
                        <span class="label label-default">
                            <xsl:choose>
                                <xsl:when test="@exclude = 'web'">仅 app 端</xsl:when>
                                <xsl:when test="@exclude = 'native'">仅 web 端</xsl:when>
                            </xsl:choose>
                        </span>
                    </xsl:if>
                </small>
            </h2>
            <p>
                <a class="btn btn-warning" target="_blank">
                    <xsl:attribute name="href">api-test.html#<xsl:value-of select="@name"/></xsl:attribute> 测试
                </a>
            </p>
            <p>URL：
                <xsl:choose>
                    <xsl:when test="url">
                        <code>
                            <xsl:value-of select="url"/>
                        </code>
                    </xsl:when>
                    <xsl:otherwise>
                        <span class="label label-danger">未指定</span>
                    </xsl:otherwise>
                </xsl:choose>
            </p>
            <p>需要签名：
                <xsl:choose>
                    <xsl:when test="login = 'true'">是</xsl:when>
                    <xsl:when test="login = 'false'">否</xsl:when>
                    <xsl:otherwise>
                        <span class="label label-danger">未指定</span>
                    </xsl:otherwise>
                </xsl:choose>
            </p>
            <xsl:apply-templates select="request"/>
            <xsl:apply-templates select="response"/>
            <xsl:apply-templates select="columnType"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="types">
        <div class="page-header">
            <h1>数据类型</h1>
        </div>
        <xsl:if test="primitive-type">
            <h2>原生类型（primitive）</h2>
            <xsl:for-each select="primitive-type">
                <xsl:apply-templates select="."/>
            </xsl:for-each>
        </xsl:if>
        <xsl:if test="class-type">
            <h2>类（class）</h2>
            <xsl:for-each select="class-type">
                <xsl:apply-templates select="."/>
            </xsl:for-each>
        </xsl:if>
    </xsl:template>
    <xsl:template match="errors">
        <div class="page-header" id="errors">
            <h1>错误代码</h1>
        </div>
        <table class="table">
            <tr>
                <th>代码</th>
                <th>说明</th>
            </tr>
            <xsl:for-each select="error">
                <tr>
                    <td>
                        <xsl:value-of select="@code"/>
                    </td>
                    <td>
                        <xsl:value-of select="."/>
                    </td>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
    <xsl:template match="request|response|columnType|primitive-type|class-type">
        <xsl:if test="*">
            <div class="panel panel-default">
                <xsl:if test="@name">
                    <xsl:attribute name="id">type-<xsl:value-of select="@name"/></xsl:attribute>
                </xsl:if>
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <xsl:choose>
                            <xsl:when test="name(.) = 'request'">请求</xsl:when>
                            <xsl:when test="name(.) = 'response'">返回</xsl:when>
                            <xsl:when test="name(.) = 'columnType'">返回字段类型</xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="@name"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </h3>
                </div>
                <xsl:if test="description or example">
                    <div class="panel-body">
                        <xsl:value-of select="description"/>
                        <xsl:for-each select="example">
                            <samp>
                                <xsl:attribute name="data-validate-against">
                                    <xsl:value-of select="../../@name"/>
                                </xsl:attribute>
                                <xsl:value-of select="."/>
                            </samp>
                        </xsl:for-each>
                    </div>
                </xsl:if>
                <table class="table">
                    <tbody>
                        <xsl:for-each select="field">
                            <tr>
                                <td>
                                    <xsl:value-of select="@name"/>
                                </td>
                                <td>
                                    <xsl:variable name="type-name">
                                        <xsl:choose>
                                            <xsl:when test="contains(@type, '[]')">
                                                <xsl:value-of select="substring-before(@type, '[]')"/>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:value-of select="@type"/>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:variable>
                                    <xsl:choose>
                                        <xsl:when test="/api-def/types/primitive-type[@name = $type-name] | /api-def/types/class-type[@name = $type-name]">
                                            <a>
                                                <xsl:attribute name="href">#type-<xsl:value-of select="$type-name"/></xsl:attribute>
                                                <xsl:value-of select="@type"/>
                                            </a>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of select="@type"/>
                                            <span class="label label-danger">类型未定义</span>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </td>
                                <td>
                                    <xsl:value-of select="."/>
                                </td>
                            </tr>
                        </xsl:for-each>
                    </tbody>
                </table>
            </div>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
